//
//  CDVPushJumpPage.m
//  Hello World
//
//  Created by jing zhao on 7/2/15.
//
//

#import "CDVPushJumpPage.h"
#import "PickerCollectionViewController.h"
#import "PickerHeader.h"

@interface CDVPushJumpPage ()<UIAlertViewDelegate>

@property ( strong, nonatomic   ) CDVInvokedUrlCommand  *commandCallBack;
@property ( strong, nonatomic   ) CDVInvokedUrlCommand  *commandCallBackAlert;
@property ( strong, nonatomic   ) CDVInvokedUrlCommand  *commandCallBackPushJumpImage;
@property ( strong, nonatomic   ) CDVInvokedUrlCommand  *commandCallBackPushJumpVideo;
@property ( strong, nonatomic   ) UIAlertView           *alertView;

@end

@implementation CDVPushJumpPage

-(void)pluginInitialize
{
    [super pluginInitialize];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushInfo:) name:NOTIFICATION_CDVPUSHJUMPPAGE object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushUpLoadSuccess:) name:NOTIFICATIONUPLOADSUCESS object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushUpLoadError:) name:NOTIFICATIONUPLOADERROR object:nil];
    
}

-(void)pushJumpToPage:(CDVInvokedUrlCommand*)command
{
    self.commandCallBack = command;
    [self keepCallBackUserDeaults];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//判断是否允许推送
-(void)boolAllowPush:(CDVInvokedUrlCommand*)command{
    BOOL boolAllowPush;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0) {
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (UIUserNotificationTypeNone != setting.types) boolAllowPush = YES;
        else boolAllowPush = NO;
    }else{
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if(UIRemoteNotificationTypeNone != type) boolAllowPush = YES;
        else boolAllowPush = NO;
    }
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:boolAllowPush];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
}
//跳转到setting 页面
-(void)topToSetting:(CDVInvokedUrlCommand*)command{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"跳转"];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
    
}

-(void)alertButtonShow:(CDVInvokedUrlCommand*)command{
    self.commandCallBackAlert = command;
    NSString *alertTitle= [command.arguments count] > 0 ? [command.arguments objectAtIndex:0] : @"";
    NSString *alertMessage = [command.arguments count] > 1 ? [command.arguments objectAtIndex:1] : @"";
    NSString *alertButton1 = [command.arguments count] > 2 ? [command.arguments objectAtIndex:2] : @"";
    NSString *alertButton2 = [command.arguments count] > 3 ? [command.arguments objectAtIndex:3] : @"";
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:alertTitle message:alertMessage delegate:self cancelButtonTitle:alertButton1 otherButtonTitles:alertButton2, nil];
    self.alertView  =alertView;
    [alertView show];
}


/********
 UIAlertViewDelegate
 ********/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self.alertView setHidden:YES];
    
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:buttonIndex];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:self.commandCallBackAlert.callbackId];
    
}
/********
 notification Center Receive
 ********/

- (void)pushInfo:(NSNotification*)notification
{
    [self keepCallBackUserDeaults];
}

-(void)keepCallBackUserDeaults
{
    if(self.commandCallBack == nil){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"推送提示" message:@"插件已经注销了！" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    NSDictionary *dic =[[NSUserDefaults standardUserDefaults]objectForKey:SAVEPUSHMESSAGE];
    if(dic){
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dic];
        [pluginResult setKeepCallbackAsBool:YES];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:self.commandCallBack.callbackId];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:SAVEPUSHMESSAGE];
    }else{
        CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        [pluginResult setKeepCallbackAsBool:YES];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:self.commandCallBack.callbackId];
    }
}
#pragma mark -pushVideo

-(void)pushVideoPage:(CDVInvokedUrlCommand*)command {
    self.commandCallBackPushJumpVideo = command;
    self.commandCallBackPushJumpImage = nil;
    NSString *userId = [command.arguments count] > 0 ? [command.arguments objectAtIndex:0] : @"";
    if ([userId isEqualToString:@""]) NSLog(@"error pushVideoPage no user Id");
    [[NSUserDefaults standardUserDefaults]setObject:userId forKey:UPLOADUSERID];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status==PHAuthorizationStatusAuthorized) {
            self.commandCallBackPushJumpVideo = command;
            PickerCollectionViewController *pickerController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"PickerController"];
            [pickerController setSelectMediaType:PHAssetMediaTypeVideo];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:pickerController];
            [self.viewController presentViewController:nav animated:YES completion:nil];
            
        }else{
            UIAlertView *alert  = [[UIAlertView alloc]initWithTitle:@"消息" message:@"用户没有授权" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
            [alert show];
        }
    }];
}

- (void)pushUpLoadSuccess:(NSNotification*)notification {
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"正确"];
    if (self.commandCallBackPushJumpVideo)
        [self.commandDelegate sendPluginResult:pluginResult callbackId:self.commandCallBackPushJumpVideo.callbackId];
    else
        [self.commandDelegate sendPluginResult:pluginResult callbackId:self.commandCallBackPushJumpImage.callbackId];
}

- (void)pushUpLoadError:(NSNotification*)notification {
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"错误"];
    if (self.commandCallBackPushJumpVideo)
        [self.commandDelegate sendPluginResult:pluginResult callbackId:self.commandCallBackPushJumpVideo.callbackId];
    else
        [self.commandDelegate sendPluginResult:pluginResult callbackId:self.commandCallBackPushJumpImage.callbackId];
}

#pragma mark -pushImage

-(void)pushImagePage:(CDVInvokedUrlCommand*)command{
    self.commandCallBackPushJumpImage = command;
    self.commandCallBackPushJumpVideo = nil;
    NSString *userId = [command.arguments count] > 0 ? [command.arguments objectAtIndex:0] : @"";
    if ([userId isEqualToString:@""]) NSLog(@"error pushVideoPage no user Id");
    [[NSUserDefaults standardUserDefaults]setObject:userId forKey:UPLOADUSERID];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status==PHAuthorizationStatusAuthorized) {
            self.commandCallBackPushJumpImage = command;
            PickerCollectionViewController *pickerController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"PickerController"];
            [pickerController setSelectMediaType:PHAssetMediaTypeImage];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:pickerController];
            [self.viewController presentViewController:nav animated:YES completion:nil];
            
        }else{
            UIAlertView *alert  = [[UIAlertView alloc]initWithTitle:@"消息" message:@"用户没有授权" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
            [alert show];
        }
    }];
}

@end
